package thread;

/**
 * 互斥锁
 */
public class SyncDehmo4 {

    public static void main(String[] args) {
        Boo b = new Boo();
        Thread t1 = new Thread("方法一："){
            public void run(){
                b.methodA();
            }
        };
        Thread t2 = new Thread("方法二："){
            public void run(){
                b.methodA();
            }
        };
        t1.start();
        t2.start();
    }
}
class Boo{
    public synchronized void methodA() {
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName() + "A方法正在执行");
            Thread.sleep(3000);
            System.out.println(t.getName() + "A方法结束");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }


    }
    public synchronized void methodB(){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+"B方法在执行");
            Thread.sleep(3000);
            System.out.println(t.getName()+"B方法结束");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
